//
//  VCPassengerManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/29.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCPassengerManger: VCBase {
    
    // MARK: 赋值
    public var userInfo:UserInfo?
    // 是否显示删除按钮
    public var isShowDelete: Bool = false
    // 是否能编辑乘机人全部信息
    public var isEditAllInfo: Bool = false
    
    lazy var createPassengerView:ViewBaseInput = {
        let ui = ViewBaseInput()
        ui.isShowBtn = true
        ui.titleLabel = "保存"
        ui.backgroundColor = UIColor.white
        ui.nameArrs = [USERNAME,EMAIL,GENDER,MOBILE,IDNUMBER]
        ui.createViews()
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCPassengerManger {
    
    func savePassenger(user:UserInfo) -> Void {
        if user.username?.count == 0 {
            self.show(text: "用户名不能为空")
            return
        }
        if user.email?.count == 0 {
            self.show(text: "邮箱不能为空")
            return
        }
        if user.mobile?.count == 0 {
            self.show(text: "手机号不能为空")
            return
        }
        if user.idnumber?.count == 0 {
            self.show(text: "身份证号不能为空")
            return
        }
        guard let userid = UserDefaults.standard.string(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(userid, forKey: SP_USERID)
        dict.setValue(user.email, forKey: SP_EMAIL)
        dict.setValue(user.mobile, forKey: SP_MOBILE)
        dict.setValue(user.gender, forKey: SP_GENDER)
        dict.setValue(user.username, forKey: SP_USERNAME)
        dict.setValue(user.idnumber, forKey: SP_IDNUMBER)
        dict.setValue(user.passengerid, forKey: SP_PASSENGERID)
        // 是否是编辑全部
        var api = "add/passenger"
        if isEditAllInfo {
            api = "edit/passenger"
        }
        // 请求
        self.doPost(api: api, dict: dict) { (dict) in
            self.show(text: "保存成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func deletePassenger(user:UserInfo) -> Void {
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(user.passengerid, forKey: SP_PASSENGERID)
        // 请求
        self.doPost(api: "delete/passenger", dict: dict) { (dict) in
            self.show(text: "删除成功")
            self.navigationController?.popViewController(animated: true)
        }
        
    }
}

// MARK: 初始化
extension VCPassengerManger {
    
    private func init_event() -> Void {
        if userInfo != nil {
            createPassengerView.userinfo = userInfo
        }
        if isShowDelete {
            createPassengerView.isShowDelete = true
        }
        // 保存按钮点击
        createPassengerView.complete = {user in
            self.savePassenger(user: user)
        }
        // 删除按钮点击
        createPassengerView.completeDelete = { user in
            self.deletePassenger(user: user)
        }
    }
    
    private func init_views() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(createPassengerView)
    }
}
